﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Wuqi.Webdiyer;

namespace Xiangmu
{
  public partial class User : System.Web.UI.Page
    {
        ly.BLL.UserInfo user = new ly.BLL.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["username"] == null)
            //{
            //    Response.Redirect("login.aspx");
            //}
            //else
            //{
                BindData();
            //}


        }
        private void BindData()
        {
            string username = search.Value.Trim();

            //string strwhere = "where Type in( ) username like '%" + username + "%'";
            string strwhere = "";
            DataTable dt = user.GetList(strwhere).Tables[0];
            PagedDataSource ps = new PagedDataSource();
            ps.DataSource = dt.DefaultView;
            ps.AllowPaging = true;
            AspNetPager1.RecordCount = dt.Rows.Count;
            ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            ps.PageSize = AspNetPager1.PageSize;
            rptUser.DataSource = ps;
            rptUser.DataBind();
        }

        protected void lbDelete_Command(object sender, CommandEventArgs e)
        {
            string username = e.CommandName;
            Boolean flag = user.Delete(username);
            if (flag)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功');</script>");
                BindData();
                //OnClientClick="return confirm('您确定要删除该记录吗?')">
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败');</script>");
                BindData();
            }
        }

        protected void btnnew_Click(object sender, EventArgs e)
        {
            Response.Redirect("addUser.aspx");
        }

        protected void btnselect_Click(object sender, EventArgs e)
        {
            //string username = (string)search.Value.Trim();
            //DataTable dt = BLL.StudentsBLL.seeksb(username);
            //PagedDataSource ps = new PagedDataSource();
            //ps.DataSource = dt.DefaultView;
            //ps.AllowPaging = true;
            //AspNetPager1.RecordCount = dt.Rows.Count;
            //ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //ps.PageSize = AspNetPager1.PageSize;
            //rptUser.DataSource = ps;
            //rptUser.DataBind();     
        }
        //private void tableDataBind()
        //{
        //    string username = search.Value.Trim();
        //    DataTable dt = BLL.StudentsBLL.GetList(username).Tables[0];
        //    PagedDataSource ps = new PagedDataSource();
        //    ps.DataSource = dt.DefaultView;
        //    ps.AllowPaging = true;
        //    AspNetPager1.RecordCount = dt.Rows.Count;
        //    ps.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        //    ps.PageSize = AspNetPager1.PageSize;
        //    rptUser.DataSource = ps;
        //    rptUser.DataBind();
        //}

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void lbDelete_select_Click(object sender, EventArgs e)
        {
            string delId = "";
            //先遍历取得选中项
            for (int i = 0; i < rptUser.Items.Count;i++ )
            {
                CheckBox cbx = (CheckBox)rptUser.Items[i].FindControl("chbqian");
                Label lbl = (Label)rptUser.Items[i].FindControl("lblid");
                //if (cbx != null)
                //{
                    if (cbx.Checked == true)
                    {
                        delId += lbl.Text.Trim() + ",";
                    }
                //}
            }
            //去掉最后一个,
            delId = delId.Replace(",)", "");
            //delId = delId.TrimEnd(',');
            //delId = delId.Remove(delId.Length-1);
            lblmsg.Text = delId;
            //bool flag = user.DeleteList(delId);
            //if (flag)
            //{
            //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('批量删除成功');</script>");
            //    BindData();
            //}
            //else
            //{
            //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('批量删除失败');</script>");
            //    BindData();
            //}
            Response.Write("删除的语句是：delete news_sosuo8_ where id_news_ in(" + delId + ")");
            //自己写删除语句吧
        }

        //protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        //{
        //    prerepater(rptUser, this);
        //}



        protected void rptNews_PreRender(object sender, EventArgs e)
        {
            prerepater(rptUser, this);
        }
        public static void prerepater(Repeater repeater, System.Web.UI.Page page)
        {
            ClientScriptManager cs = page.ClientScript;
            for (int i = 0; i < repeater.Items.Count; i++)
            {
                CheckBox cbx = (CheckBox)repeater.Items[i].FindControl("chbqian");
                //将相应的服务器控件的ClientId注册到客户端JavaScript数组
                cs.RegisterArrayDeclaration("cbxArray", String.Concat("'", cbx.ClientID, "'"));
            }
        }
    }
}